﻿#include "widget.h"
#include "ui_widget.h"
#include "imageslider.h"
#include "custontheme.h"
#include "SerialPortUtil.h"
#include "cam.h"
#include "ColorSingleton.h"
#include <QPainter>

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define PINK 3

//项目思路：多个自定义类继承QWidget，开灯开风扇由类自定义信号与槽，去触发将数据传给串口
//串口写入，开发板里自带的程序检测到串口数据就会产生对应的操作，比如控制GPIO口

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 设置无标题栏
    setWindowFlags(Qt::FramelessWindowHint);
    // 设置背景透明以支持圆角
    setAttribute(Qt::WA_TranslucentBackground);

    themeWidget = new CustonTheme(this);
    themeWidget->hide();
    buttonGroup = new QButtonGroup(this);
    contrlUtil = new ContrlUtils(this);
    contrlUtil->hide();
    camWidget = new Cam(this);
    camWidget->hide();

    SerialPortUtil::initSerialPort("COM4", 9600);

    Mycolor::setThemeIndex(BLACK);
    Mycolor::setColorList();

    QStringList imagePaths = {
        ":/res/backg01.png",
        ":/res/backg02.png",
        ":/res/backg03.png",
        ":/res/backg04.png",
        ":/res/bg005.png"
    };
    ui->widgetShowCen01->setImages(imagePaths);
    ui->widgetShowCen01->setChangeInterval(3000); // 每 3000 毫秒切换图片

    //设置按键组互斥

    buttonGroup->addButton(ui->btnCam);
    buttonGroup->addButton(ui->btnHome);
    buttonGroup->addButton(ui->btnTheme);
    buttonGroup->addButton(ui->btnDev);
    buttonGroup->setExclusive(true);
    //程序左侧按钮控件的信号与槽
    connect(ui->widgetCen02,&ContrlUtils::lightCheckClicked,this,&Widget::lightCon);
    connect(ui->widgetCen02,&ContrlUtils::shanCheckClicked,this,&Widget::shanCon);
    connect(ui->widgetCen02,&ContrlUtils::airCheckClicked,this,&Widget::airCon);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QPainterPath path;
    path.addRoundedRect(rect(), 35, 35); // 15是圆角半径
    painter.fillPath(path, QColor(201,201,201)); // 您可以选择所需的颜色

}

void Widget::on_btnDev_clicked()
{

    if(contrlUtil){
        contrlUtil->setGeometry(450,160,contrlUtil->width(),contrlUtil->height());
        contrlUtil->show();
    }
    if(themeWidget) themeWidget->hide();
    if(camWidget) camWidget->hide();

}

void Widget::on_btnCam_clicked()
{
    camWidget->setGeometry(110,10,camWidget->width(),camWidget->height());
    camWidget->show();

    if(contrlUtil) contrlUtil->hide();
    if(themeWidget) themeWidget->hide();

}

void Widget::on_btnHome_clicked()
{
    //页面的显示和遮挡，最好用单例模式，这里就简单的控制，不太友好
    if(contrlUtil ) contrlUtil->hide();
    if(themeWidget) themeWidget->hide();
    if(camWidget  ) camWidget->hide();
}

void Widget::lightCon(bool value)
{
    static int data = 0;
    data++;
    qDebug() << "clicked " << data;

    QWidget *contrl = ui->widgetCen02->findChild<QWidget*>("widgetTop");
    QWidget *contrlLight = contrl->findChild<QWidget*>("widgetLight");
    QPushButton *btn = contrlLight->findChild<QPushButton *>("btnLight_2");
    if(value){
        contrlLight->setStyleSheet("background-color: rgb(200, 200, 200)");
        btn->setStyleSheet("border-image: url(:/con/contrl/dd2.png);");
        SerialPortUtil::lightCon();
    }else{
        btn->setStyleSheet("border-image: url(:/con/contrl/dd1.png);");
        setBtnColor(contrlLight);
        SerialPortUtil::lightCon();
    }


}

//主题相关API
void Widget::on_btnTheme_clicked()
{

    themeWidget->setGeometry(110,10,themeWidget->width(),themeWidget->height());
    themeWidget->show();//让组件跳出
    connect(themeWidget,&CustonTheme::onThemeBlueSet,this,[=](){
        Mycolor::setThemeIndex(BLUE);
        ColorSingleton::getInstance().setColor(Mycolor::blueColorList.at(4));
        setTheme();
    });
    connect(themeWidget,&CustonTheme::onThemeBlackSet,this,[=](){
        Mycolor::setThemeIndex(BLACK);
        ColorSingleton::getInstance().setColor(Mycolor::blackColorList.at(4));
        setTheme();
    });
    connect(themeWidget,&CustonTheme::onThemePinkSet,this,[=](){
        Mycolor::setThemeIndex(PINK);
        ColorSingleton::getInstance().setColor(Mycolor::pinkColorList.at(4));
        setTheme();
    });
    connect(themeWidget,&CustonTheme::onThemeGreenSet,this,[=](){
        Mycolor::setThemeIndex(GREEN);
        ColorSingleton::getInstance().setColor(Mycolor::greenColorList.at(4));
        setTheme();
    });

    if(contrlUtil ) contrlUtil->hide();
    if(camWidget) camWidget->hide();

}

//主题相关API
void Widget::shanCon(bool value)
{
    QWidget *contrl = ui->widgetCen02->findChild<QWidget*>("widgetTop");
    QWidget *widWind = contrl->findChild<QWidget*>("widgetWind");
    QPushButton *btnShan = widWind->findChild<QPushButton *>("btnShan");
    if(value){
        widWind->setStyleSheet("background-color: rgb(200, 200, 200)");
        btnShan->setStyleSheet("border-image: url(:/con/contrl/fs2.png);");
    }else{
        btnShan->setStyleSheet("border-image: url(:/con/contrl/fs1.png);");
        setBtnColor(widWind);
    }
}
//主题相关API
void Widget::airCon(bool value)
{
    QWidget *contrl = ui->widgetCen02->findChild<QWidget*>("widgetTop");
    QWidget *widAir = contrl->findChild<QWidget*>("widgetAir");
    QPushButton *btnAir = widAir->findChild<QPushButton *>("btnAir");
    if(value){
        widAir->setStyleSheet("background-color: rgb(200, 200, 200)");
        btnAir->setStyleSheet("border-image: url(:/con/contrl/kt2.png);");
    }else{
        btnAir->setStyleSheet("border-image: url(:/con/contrl/kt1.png);");
        setBtnColor(widAir);
    }
}

//主题相关API
void Widget::setTheme()
{
    QStringList colors;
    switch (Mycolor::myTheme) {
    case BLACK:
        colors = Mycolor::blackColorList;
        break;
    case BLUE:
        colors = Mycolor::blueColorList;
        break;
    case PINK:
        colors = Mycolor::pinkColorList;
        break;
    case GREEN:
        colors = Mycolor::greenColorList;
        break;
    }

    ui->widgetALL->setStyleSheet(colors.at(1));
    ui->widgetCenter->setStyleSheet(colors.at(2));

    QWidget *contrl = ui->widgetCen02->findChild<QWidget*>("widgetTop");
    contrl->setStyleSheet(colors.at(3));
    contrl->findChild<QWidget*>("widgetLight")->setStyleSheet(colors.at(4));
    contrl->findChild<QWidget*>("widgetWind")->setStyleSheet(colors.at(4));
    contrl->findChild<QWidget*>("widgetAir")->setStyleSheet(colors.at(4));

    QWidget *wea = ui->widgetWea->findChild<QWidget*>("widgetTop");
    wea->setStyleSheet(colors.at(3));
    wea->findChild<QWidget*>("widgetdays")->setStyleSheet(colors.at(4));

    QWidget *music = ui->widgetMusic->findChild<QWidget*>("widgetTop");
    music->setStyleSheet(colors.at(3));

    QWidget *cur = ui->widgetCur->findChild<QWidget*>("widgetTop");
    cur->setStyleSheet(colors.at(3));

    on_btnHome_clicked();
    ui->btnHome->setChecked(true);


}
//主题相关API
void Widget::setBtnColor(QWidget *btn)
{
    switch (Mycolor::myTheme) {
    case BLACK:
        btn->setStyleSheet(Mycolor::blackColorList.at(4));
        break;
    case BLUE:
        btn->setStyleSheet(Mycolor::blueColorList.at(4));
        break;
    case PINK:
        btn->setStyleSheet(Mycolor::pinkColorList.at(4));
        break;
    case GREEN:
        btn->setStyleSheet(Mycolor::greenColorList.at(4));
        break;
    }
}



